/******************************************************************
 * Created by: GOh CHee Hong                                      
 * Date of Creation: 1/Jan/2009            
 
 *                                                                
 * Description: This class is in charge of downloading a message a message
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Threading;
using Pbdms.Common.SharedVariables;
using Pbdms.Common.CryptoUtils;
namespace Pbdms.BusinessLogic.Mail
{
    public class MessageDownloader 

    {
        private string messageInfo;
        private SecretKeyControl secretKeyControl;
        private PublicKeysControl publicKeyControl;
        private MemoryStream memoryStorage;
        private Boolean downloadFinished=false;
        private Message message = null;
        public Message Message
        {
            get
            {
                return message;
            }
        }
        public Boolean DownloadFinished
        {
            get
            {
                return downloadFinished;
            }
        }
        public MessageDownloader()
        {
            downloadFinished = false;
        }
        ~MessageDownloader()
        {
            if (memoryStorage != null)
            {
                memoryStorage.Close();
            }
            
        }
        public MessageDownloader(string information, SecretKeyControl secretKeyControl, PublicKeysControl publicKeyControl)
        {
            this.secretKeyControl = secretKeyControl;
            this.publicKeyControl = publicKeyControl;
            this.messageInfo = information;
            downloadFinished=false;

        }
        public void DownloadMessage()
        {
            try
            {
                OpenDHTLib.OpenLookUp openDHT = new Pbdms.OpenDHTLib.OpenLookUp();
                
                string mailKey = Common.Global.filerMailBoxKey(messageInfo);
                long size = Common.Global.filterSize(messageInfo);
                //memoryStorage = new MemoryStream((int)size);
                memoryStorage = new MemoryStream();
                //calculate the number of blocks
                int blocks = Common.Global.CalculateNoOfBlocks(size);
                byte[] data = null;
                //if more than 1 block
                for (int i = 0; i < blocks; i++)
                {
                    data = openDHT.GetARawValue(mailKey + " Block No:" + i);
                    if (data != null)
                    {
                        memoryStorage.Write(data, 0, data.Length);
                    }
                    else
                    {
                        //cant retrieve data
                        return;
                    }
                } 
                //complete  the message
                //deserialisation it
                try
                {
                    Decrypter decrypter = new Decrypter();
                    //MemoryStream decrypStream = new MemoryStream();
                    memoryStorage.Seek(0, SeekOrigin.Begin);
                    //verify the object
                    Verifier verifier = new Verifier(publicKeyControl);
                    Common.DataObject.VerifyObjMem verifyMem = verifier.VerifyBytes(memoryStorage);            
                    //decrypt the object
                    memoryStorage = decrypter.DecryptBytes(verifyMem.MemData, secretKeyControl.readPrivateKey());
                    //deserialize
                    BinaryFormatter formatter = new BinaryFormatter();
                    message = (Message)formatter.Deserialize(memoryStorage);
                    message.sender = verifyMem.keyID;
                    message.size = size;
                    message.isRead = false;
                    downloadFinished = true;
                }
                catch
                {

                }
     

            }
            catch
            {

            }
            finally
            {
                if (memoryStorage != null)
                {
                    memoryStorage.Close();
                }
               
            }
        }
    }
}
